URL='http://localhost:8601/';
var new_element=document.createElement("script");
new_element.setAttribute("type","text/javascript");
new_element.setAttribute("src","js/layui/layui.js");
document.body.appendChild(new_element);
var user2 = localStorage.getExpire("user");
var user=JSON.stringify(user2);
console.log("tree.js.user"+user);
$(function () {
    var token= localStorage.getExpire("token");
    console.log("tree.js.token"+token);
    if(token==null || token==""){
        layui.use('layer', function(){
            layer.msg("用户信息失效请重新登录,即将进入登录页面......", {time: 3000});
            var i=3;
            time=setInterval(function () {
                i--;
                if(i==0){
                    clearInterval(time);
                    window.location.href="../adview/login.html";
                }
            },1000)
        })
        return;
    }
    layui.use(['element','layer','jquery','tree'], function(){
        var element = layui.element;
        var tree = layui.tree;
        var layer = layui.layer;
        var $ = layui.jquery;
    $.ajax({
        headers:{"Authorization":token},
        type: "post",
        url: URL+"getTree",
        dataType: "json",
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            console.log(user+"user");
            console.info(data);//tree.render
            console.log(layui.v);
            tree.render({
                elem: '#demo',// 传入元素选择器
                data: data,
                showLine:false,
                click: function (node) {// 点击tree菜单项的时候
                    console.log(node);
                    var exist = $("li[lay-id='" + node.data.menu_id + "']").length;//判断是不是用重复的选项卡
                    console.log(node.data.menu_id);
                    console.log(exist);
                    if (exist > 0) {
                        element.tabChange('tabs', node.data.menu_id);// 切换到已有的选项卡
                    } else {
                     /*
                     *onload方法 高度自适应 根据内容来伸缩的。
                     * */
                        if (node.data.menu_url != null && node.data.menu_url != "") {// 判断是否需要新增选项卡
                            element.tabAdd('tabs',{
                                    title: node.data.title,
                                    content: '<iframe scrolling="yes" frameborder="0" src="'
                                    + node.data.menu_url
                                    + '" width="100%" height="400px" onload=\' this.style.height=Math.max(this.contentWindow.document.body.scrollHeight,this.contentWindow.document.documentElement.scrollHeight,200)+"px";\'\n' +
                                    '\n' +
                                    '————————————————\n' +
                                    '></iframe>'// 支持传入html
                                    ,
                                    id: node.data.menu_id
                                });
                            element.tabChange('tabs', node.data.menu_id);
                        }
                    }

                }

            });

        }

    });
        element.render();
})
    })